package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.api.business.entity.RobotMapAreaEntity;
import com.hitqz.robot.api.business.entity.RobotMapAreaPosEntity;
import com.hitqz.robot.api.business.vo.RobotMapAreaPosVo;
import com.hitqz.robot.api.business.vo.RobotMapAreaVo;
import com.hitqz.robot.biz.service.RobotMapAreaPosService;
import com.hitqz.robot.biz.service.RobotMapAreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xupkun
 * @date 2024/6/6
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/client/mapArea" )
@Tag(description = "client" , name = "获取机器人地图的区域" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RobotMapAreaController {


	private final RobotMapAreaService robotMapAreaService;

	private final RobotMapAreaPosService robotMapAreaPosService;

	@GetMapping("/{groupId}")
	@Operation(summary = "获取所有地图区域数据" , description = "当前机器人分组下的区域数据" )
	public R<List<RobotMapAreaVo>> getMapArea(@PathVariable Integer groupId) {
		List<RobotMapAreaEntity> mapAreaEntities = robotMapAreaService
				.list(Wrappers.<RobotMapAreaEntity>lambdaQuery().eq(RobotMapAreaEntity::getGroupId, groupId));
		List<RobotMapAreaVo> areaVoLis = Lists.newArrayList();
		for (RobotMapAreaEntity areaEntity: mapAreaEntities) {
			RobotMapAreaVo vo = new RobotMapAreaVo();
			BeanUtil.copyProperties(areaEntity, vo);
			vo.setLightStatus(1);
			List<RobotMapAreaPosEntity> posEntities= robotMapAreaPosService
					.list(Wrappers.<RobotMapAreaPosEntity>lambdaQuery().eq(RobotMapAreaPosEntity::getAreaId, areaEntity.getId()));
			List<RobotMapAreaPosVo> posVos = Lists.newArrayList();
			posEntities.stream().forEach(t-> {
				RobotMapAreaPosVo posVo = new RobotMapAreaPosVo();
				BeanUtil.copyProperties(t, posVo);
				posVos.add(posVo);
			});
			vo.setPos(posVos);
			areaVoLis.add(vo);
		}
		return R.ok(areaVoLis);
	}


}
